/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import sql.Connect;

/**
 *
 * @author Lunardrik
 */
public class Generator {
    public static String xmlGen(String filePath) {
        {
            FileWriter fw = null;
            try {
                Connect conMgr = new Connect();
                Connection con = conMgr.getConnection();
                Statement sta = con.createStatement();
                ResultSet rs = sta.executeQuery("SELECT (SELECT CarID, Model, CAST(Price AS INT) AS Price, Description, YearProduced, Picture, CAST(ProducerName AS NVARCHAR(50)) FROM (Car LEFT JOIN Producer ON Car.ProducerID = Producer.ProducerID) FOR XML AUTO, ELEMENTS, ROOT('Cars'))");
                rs.next();
                String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + rs.getNString(1);
                File f = new File(filePath + "/cars.xml");
                f.delete();
                f.createNewFile();
                System.out.println(f.getPath());
                fw = new FileWriter(f);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(new String(result.getBytes(),"UTF-8"));
                bw.close();
                
                return result;
            } catch (SQLException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fw.close();
                } catch (IOException ex) {
                    Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
        }
    }
    
    public static String getModels()
    {
        try {
            Connect conMgr = new Connect();
            Connection con = conMgr.getConnection();
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery("SELECT (SELECT Model FROM Car FOR XML AUTO, ELEMENTS, ROOT('Cars'))");
            rs.next();
            String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + rs.getNString(1);

            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    }
    
    public static String getBrands()
    {
        try {
            Connect conMgr = new Connect();
            Connection con = conMgr.getConnection();
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery("SELECT (SELECT ProducerName FROM Producer FOR XML AUTO, ELEMENTS, ROOT('Cars'))");
            rs.next();
            String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + rs.getNString(1);

            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    }
    
    public static String xmlTransformWithParams(String mxsltFile, String mxmlFile, Dictionary<String, String> params) throws TransformerConfigurationException, TransformerException {        
        TransformerFactory tf = TransformerFactory.newInstance();
        StreamSource xsltFile = new StreamSource(new File(mxsltFile));
        Transformer trans = tf.newTransformer(xsltFile);
        
        if (params != null)
        {
            Enumeration<String> keys = params.keys();
            Enumeration<String> values = params.elements();
            
            while (keys.hasMoreElements())
            {
                trans.setParameter(keys.nextElement(), values.nextElement());
            }
        }
        
        StreamSource xmlFile = new StreamSource(new File(mxmlFile));
        String htmlContent = "";
        StringWriter html = new StringWriter();
        StreamResult rs = new StreamResult(html);

        trans.transform(xmlFile, rs);
        
        return html.toString();    
    }
    
    public static String xmlTransform(String mxsltFile, String mxmlFile) throws TransformerConfigurationException, TransformerException {
        return xmlTransformWithParams(mxsltFile, mxmlFile, null);
    }
}
